package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Education;
import com.personal.portal.admin.mapper.EducationMapper;
import com.personal.portal.admin.service.EducationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EducationServiceImpl implements EducationService {

    @Resource
    private EducationMapper educationMapper;

    @Override
    public Education queryById(Long id) {
        validateId(id);
        return educationMapper.selectOneById(id);
    }

    @Override
    public Education queryByEntity(Education education) {
        QueryWrapper queryWrapper = buildQueryWrapper(education);
        return educationMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<Education> queryList(Education education) {
        QueryWrapper queryWrapper = buildQueryWrapper(education);
        return educationMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<Education> queryPage(int pageNum, int pageSize, Education education) {
        QueryWrapper queryWrapper = buildQueryWrapper(education);
        Page<Education> paginate = educationMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(Education education) {
        validateEducation(education);
        return educationMapper.insert(education);
    }

    @Override
    public int update(Education education) {
        if (education.getId() == null) {
            throw new IllegalArgumentException("教育背景ID不能为空");
        }
        validateId(education.getId());
        validateEducation(education);
        
        // 检查是否存在
        Education existing = educationMapper.selectOneById(education.getId());
        if (existing == null) {
            throw new IllegalArgumentException("教育背景信息不存在");
        }
        
        return educationMapper.update(education);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        // 检查是否存在
        Education existing = educationMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("教育背景信息不存在");
        }
        
        return educationMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new java.util.ArrayList<>();
        List<Long> failedIds = new java.util.ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                // 检查是否存在
                Education existing = educationMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = educationMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    

    /**
     * 验证教育背景信息
     */
    private void validateEducation(Education education) {
        if (education == null) {
            throw new IllegalArgumentException("教育背景信息不能为空");
        }
        if (StringUtils.isBlank(education.getSchool())) {
            throw new IllegalArgumentException("学校名称不能为空");
        }
        if (StringUtils.isBlank(education.getMajor())) {
            throw new IllegalArgumentException("专业名称不能为空");
        }
        if (StringUtils.isBlank(education.getDegree())) {
            throw new IllegalArgumentException("学位信息不能为空");
        }
        if (StringUtils.isBlank(education.getLevel())) {
            throw new IllegalArgumentException("学历层次不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的教育背景ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(Education education) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Education::getId, education.getId(), education.getId() != null);
        queryWrapper.like(Education::getSchool, education.getSchool(), StringUtils.isNotEmpty(education.getSchool()));
        queryWrapper.like(Education::getMajor, education.getMajor(), StringUtils.isNotEmpty(education.getMajor()));
        queryWrapper.like(Education::getDegree, education.getDegree(), StringUtils.isNotEmpty(education.getDegree()));
        queryWrapper.like(Education::getLevel, education.getLevel(), StringUtils.isNotEmpty(education.getLevel()));
        queryWrapper.ge(Education::getStartDate, education.getStartDate(), StringUtils.isNotEmpty(education.getStartDate()));
        queryWrapper.le(Education::getEndDate, education.getEndDate(), StringUtils.isNotEmpty(education.getEndDate()));

        queryWrapper.orderBy(Education::getStartDate, true);
        queryWrapper.orderBy(Education::getEndDate, true);
        queryWrapper.orderBy(Education::getId, true);
        return queryWrapper;
    }
}
